/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.clientpackets;


import l1j.opqlo.Server.L1World.L1World;
import l1j.server.echo.ClientThread;
import l1j.server.server.datatables.PetItemTable;
import l1j.server.server.datatables.PetTypeTable;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.Instance.L1PetInstance;
import l1j.server.server.serverpackets.S_PetEquipment;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.templates.L1PetItem;
import l1j.server.server.templates.L1PetType;

import java.util.logging.Level;
import java.util.logging.Logger;
/**
 * 處理收到由客戶端傳來使用寵物道具的封包
 */
public class C_UsePetItem extends ClientBasePacket {

	/**
	 * 【Client】 id:60 size:8 time:1302335819781
	 * 0000	3c 00 04 bd 54 00 00 00                            <...T...
	 *
	 * 【Server】 id:82 size:16 time:1302335819812
	 * 0000	52 25 00 04 bd 54 00 00 0a 37 80 08 7e ec d0 46    R%...T...7..~..F
	 */
	private static final Logger _log = Logger.getLogger(C_UsePetItem.class.getName());
	private static final String C_USE_PET_ITEM = "[C] C_UsePetItem";

	@Override
	public String getType() {
		return C_USE_PET_ITEM;
	}

	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			this.read(decrypt);

			final L1PcInstance pc = client.getActiveChar();
			if (pc == null) {
				return;
			}

			final int data = this.readC();
			final int petId = this.readD();
			final int listNo = this.readC();

			final L1PetInstance pet = (L1PetInstance) L1World.getInstance().findObject(petId);

			if (pet == null) {
				return;
			}
			final L1ItemInstance item = pet.getInventory().getItems().get(listNo);
			if (item == null) {
				return;
			}

			if (item.getItem().getType2() == 0
					&& item.getItem().getType() == 11) { // 寵物道具
				final L1PetType petType = PetTypeTable.getInstance().get(pet.getNpcTemplate().get_npcId());
				// 判斷是否可用寵物裝備
				if (!petType.canUseEquipment()) {
					pc.sendPackets(new S_ServerMessage(74, item.getLogName()));
					return;
				}
				final int itemId = item.getItem().getItemId();
				final L1PetItem petItem = PetItemTable.getInstance().getTemplate(itemId);
				if (petItem.getUseType() == 1) { // 牙齒
					pet.usePetWeapon(pet, item);
					pc.sendPackets(new S_PetEquipment(data, pet, listNo)); // 裝備時更新寵物資訊
				} else if (petItem.getUseType() == 0) { // 盔甲
					pet.usePetArmor(pet, item);
					pc.sendPackets(new S_PetEquipment(data, pet, listNo)); // 裝備時更新寵物資訊
				} else {
					pc.sendPackets(new S_ServerMessage(74, item.getLogName()));
				}
			} else {
				pc.sendPackets(new S_ServerMessage(74, item.getLogName()));
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}
	}
}
